package service.edu.controller;


import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import service.edu.client.VodClient;
import service.edu.entity.Video;
import service.edu.service.VideoService;
import service.edu.result.R;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author diao
 * @since 2020-08-05
 */
@RestController
@CrossOrigin
@RequestMapping("/edu/video")
public class VideoController {
    @Autowired
    private VideoService videoService;
    @Autowired
    private VodClient vodClient;

    @ApiOperation("添加小节")
    @PostMapping("addVideo")
    public R addVideo(@ApiParam(name = "video",value = "新增的小节")@RequestBody(required = false) Video video){
        videoService.save(video);
        return R.Ok();
    }

    @ApiOperation("修改小节")
    @PostMapping("updateVideo")
    public R updateVideo(@ApiParam(name = "video",value = "修改的小节")@RequestBody(required = false) Video video){
        videoService.updateById(video);
        return R.Ok();
    }

    @ApiOperation("删除小节")
    @DeleteMapping("deleteVideo/{videoId}")
    public  R deleteVideoById(@ApiParam(name = "videoId",value = "小节对应的id")@PathVariable String videoId){
        //根据小节id获得视频id
        Video video = videoService.getById(videoId);
        String videoSourceId = video.getVideoSourceId();
        //根据视频id删除视频
        if (!StringUtils.isEmpty(videoSourceId))
        {vodClient.deleteVideoByVideoId(videoSourceId);}
        //删除小节
        videoService.removeById(videoId);
        return R.Ok();
    }

    @ApiOperation("根据小节id查询小节")
    @GetMapping("queryVideo/{videoId}")
    public R queryVideoById(@ApiParam(name = "videoId",value = "小节对应的id")@PathVariable("videoId")String videoId){
        Video video = videoService.getById(videoId);
        return R.Ok().data("video", video);
    }

}

